package com.um.psystem.service.mtService.impl;

import cn.hutool.core.util.StrUtil;
import com.um.psystem.common.Const;
import com.um.psystem.entity.ExcelHeader;
import com.um.psystem.enums.ResponseEnum;
import com.um.psystem.mapper.platform.PublicMapper;
import com.um.psystem.model.sysModel.response.UserResponse;
import com.um.psystem.model.vo.JsonResult;
import com.um.psystem.service.mtService.IAssetsApplyService;
import com.um.psystem.utils.EasyExcelUtils;
import com.um.psystem.utils.RTX;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @Author: zzj
 * @Description: 物资申请实现类
 * @Date: 2020/6/12
 */
@Service
public class AssetsApplyService implements IAssetsApplyService {

    @Autowired
    PublicMapper publicMapper;

    @Override
    public JsonResult<Integer> save(Map map) {
        Map paramsMap = new HashMap();
        String dept_id = map.get("dept_id").toString();
        String dept_name = map.get("dept_name").toString();
        String operator_node = map.get("operator_node").toString();
        String operator_info = map.get("operator_info").toString();
        String sequence = map.get("sequence").toString();
        String user_name = map.get("user_name").toString();
        String sql = " insert into ws_eng_assets_operator_config(dept_id,dept_name,operator_node,operator_info,"
                + " sequence,user_name) "
                + " select #{dept_id},#{dept_name},#{operator_node},#{operator_info},#{sequence},#{user_name}";
        paramsMap.put("isqlStr", sql);
        paramsMap.put("dept_id", dept_id);
        paramsMap.put("dept_name", dept_name);
        paramsMap.put("operator_node", operator_node);
        paramsMap.put("operator_info", operator_info);
        paramsMap.put("sequence", sequence);
        paramsMap.put("user_name", user_name);

        return JsonResult.success(publicMapper.saveItems(paramsMap));
    }

    public JsonResult<Integer> update(Map map) {
        Map paramsMap = new HashMap();
        String dept_id = map.get("dept_id").toString();
        String dept_name = map.get("dept_name").toString();
        String operator_node = map.get("operator_node").toString();
        String operator_info = map.get("operator_info").toString();
        String sequence = map.get("sequence").toString();
        String user_name = map.get("user_name").toString();
        String id = map.get("id").toString();
        String sql = " update ws_eng_assets_operator_config "
                + " set dept_id=#{dept_id},dept_name=#{dept_name},operator_node=#{operator_node},"
                + " operator_info=#{operator_info},sequence=#{sequence},user_name=#{user_name} "
                + " where id=#{id}";
        paramsMap.put("usqlStr", sql);
        paramsMap.put("dept_id", dept_id);
        paramsMap.put("dept_name", dept_name);
        paramsMap.put("operator_node", operator_node);
        paramsMap.put("operator_info", operator_info);
        paramsMap.put("sequence", sequence);
        paramsMap.put("user_name", user_name);
        paramsMap.put("id", id);

        return JsonResult.success(publicMapper.updateItems(paramsMap));
    }

    @Override
    public JsonResult<Integer> del(Map map) {
        Integer id = Integer.parseInt(map.get("id").toString());
        Map paramsMap = new HashMap();
        String sql = " delete from ws_eng_assets_operator_config where id=#{id}";
        paramsMap.put("dsqlStr", sql);
        paramsMap.put("id", id);
        return JsonResult.success(publicMapper.delItems(paramsMap));
    }

    public JsonResult<Integer> save_apply(Map map) {
        Map paramsMap = new HashMap();
        String erp_no = "";
        String apply_dept = "";
        String apply_man = "";
        String out_type = "";
        String apply_num = "";
        String return_plandate = "";
        String assets_id = "";
        String node_seq = "";
        String turn_node_seq = "";
        String node_name = "";
        String flow_id = "";
        String operator = "";
        String demand_time = "";
        String stock_num = "";
        String apply_reason = "";
        String remark = "";
        String state = "";
        String apply_order_info_id = "";
        String urgent_flag = "";
        String generic_flag = "";
        String use_to = "";
        String for_workshop = "";
        String dept_use = "";
        if (StrUtil.isNotBlank(map.get("erpInfo") != null ? map.get("erpInfo").toString() : null)) {
            erp_no = map.get("erpInfo").toString();
        }
        if (StrUtil.isNotBlank(map.get("apply_man") != null ? map.get("apply_man").toString() : null)) {
            apply_man = map.get("apply_man").toString();
        }
        if (StrUtil.isNotBlank(map.get("out_type") != null ? map.get("out_type").toString() : null)) {
            out_type = map.get("out_type").toString();
        }
        if (StrUtil.isNotBlank(map.get("apply_num") != null ? map.get("apply_num").toString() : null)) {
            apply_num = map.get("apply_num").toString();
        }
        if (StrUtil.isNotBlank(map.get("return_plandate") != null ? map.get("return_plandate").toString() : null)) {
            return_plandate = map.get("return_plandate").toString();
        }
        if (StrUtil.isNotBlank(map.get("assets_id") != null ? map.get("assets_id").toString() : null)) {
            assets_id = map.get("assets_id").toString();
        }
        if (StrUtil.isNotBlank(map.get("stock_num") != null ? map.get("stock_num").toString() : null)) {
            stock_num = map.get("stock_num").toString();
        }
        if (StrUtil.isNotBlank(map.get("demand_time") != null ? map.get("demand_time").toString() : null)) {
            demand_time = map.get("demand_time").toString();
        }
        if (StrUtil.isNotBlank(map.get("apply_reason") != null ? map.get("apply_reason").toString() : null)) {
            apply_reason = map.get("apply_reason").toString();
        }
        if (StrUtil.isNotBlank(map.get("remark") != null ? map.get("remark").toString() : null)) {
            remark = map.get("remark").toString();
        }
        if (StrUtil.isNotBlank(map.get("state") != null ? map.get("state").toString() : null)) {
            state = map.get("state").toString();
        }
        if (StrUtil.isNotBlank(map.get("urgent_flag") != null ? map.get("urgent_flag").toString() : null)) {
            urgent_flag = map.get("urgent_flag").toString();
        }
        if (StrUtil.isNotBlank(map.get("generic_flag") != null ? map.get("generic_flag").toString() : null)) {
            generic_flag = map.get("generic_flag").toString();
        }
        if (StrUtil.isNotBlank(map.get("use_to") != null ? map.get("use_to").toString() : null)) {
            use_to = map.get("use_to").toString();
        }
        if (StrUtil.isNotBlank(map.get("for_workshop") != null ? map.get("for_workshop").toString() : null)) {
            for_workshop = map.get("for_workshop").toString();
        }
        if (StrUtil.isNotBlank(map.get("dept_use") != null ? map.get("dept_use").toString() : null)) {
            dept_use = map.get("dept_use").toString();
        }
        apply_man = SecurityUtils.getSubject().getPrincipal().toString();
        String sql = "select a.dept_name from ws_department a "
                + "left join( "
                + "select b.dept_name,dept_group_id from ws_employee a "
                + "join ws_department b "
                + "on a.depart_no = b.dept_no "
                + "where a.emp_name=#{emp_name} and a.is_work='1')b "
                + "on FIND_IN_SET(a.dept_id,b.dept_group_id) "
                + "where b.dept_group_id is not null and a.dept_grade='2'";
        paramsMap.put("sqlStr", sql);
        paramsMap.put("emp_name", apply_man);
        List<LinkedHashMap<String, Object>> dept_list = publicMapper.getPublicItems(paramsMap);
        if (dept_list.size() > 0) {
            apply_dept = dept_list.get(0).get("dept_name").toString();
        } else {
            return JsonResult.error(ResponseEnum.SERVER_ERROR);
        }
        paramsMap.clear();
        int saveNum = 0;
        if (StrUtil.isNotBlank(map.get("id") != null ? map.get("id").toString() : null)) {
            apply_order_info_id = map.get("id").toString();
        } else {
            sql = " insert into ws_eng_assets_apply_order_info values (null,#{erp_no},#{apply_dept},#{apply_man},now()," +
                    "#{state},'',#{urgent_flag}) ";
            paramsMap.put("isqlStr", sql);
            paramsMap.put("erp_no", erp_no);
            paramsMap.put("apply_dept", apply_dept);
            paramsMap.put("apply_man", apply_man);
            paramsMap.put("state", state);
            paramsMap.put("urgent_flag", urgent_flag);
            saveNum = publicMapper.saveItems(paramsMap);
            paramsMap.clear();
            sql = "select * from ws_eng_assets_apply_order_info where erp_no = #{erp_no} " +
                    "and apply_name = #{apply_man} order by apply_time desc limit 1";
            paramsMap.put("sqlStr", sql);
            paramsMap.put("erp_no", erp_no);
            paramsMap.put("apply_man", apply_man);
            List<LinkedHashMap<String, Object>> apply_order_list = publicMapper.getPublicItems(paramsMap);
            apply_order_info_id = apply_order_list.get(0).get("id").toString();
            paramsMap.clear();
        }
        sql = "  insert into ws_eng_assets_apply_info(erp_no,apply_order_info_id,apply_dept,apply_man,out_type,"
                + " apply_num,remark,apply_time,state,currentuser,operator_record,"
                + "assets_id,node_seq,turn_node_seq,demand_time,stock_num,apply_reason,generic_flag,use_to,for_workshop,dept_use) "
                + " select #{erp_no},#{apply_order_info_id},#{apply_dept},#{apply_man},#{out_type},#{apply_num},#{remark},now(),"
                + " #{state},#{currentuser},#{operator_record},#{assets_id},#{node_seq},#{turn_node_seq},#{demand_time},#{stock_num},#{apply_reason}," +
                " #{generic_flag},#{use_to},#{for_workshop},#{dept_use}";
        paramsMap.put("isqlStr", sql);
        paramsMap.put("erp_no", erp_no);
        paramsMap.put("apply_order_info_id", apply_order_info_id);
        paramsMap.put("apply_dept", apply_dept);
        paramsMap.put("apply_man", apply_man);
        paramsMap.put("out_type", out_type);
        paramsMap.put("apply_num", apply_num);
        paramsMap.put("state", state);
        paramsMap.put("currentuser", apply_man);
        paramsMap.put("operator_record", "申请(" + apply_man + ")");
        paramsMap.put("assets_id", assets_id);
        paramsMap.put("node_seq", "-1");
        paramsMap.put("turn_node_seq", "1");
        paramsMap.put("demand_time", demand_time);
        paramsMap.put("stock_num", stock_num);
        paramsMap.put("apply_reason", apply_reason);
        paramsMap.put("remark", remark);
        paramsMap.put("generic_flag", generic_flag);
        paramsMap.put("use_to", use_to);
        paramsMap.put("for_workshop",for_workshop);
        paramsMap.put("dept_use",dept_use);
        if (saveNum == 1 || !apply_order_info_id.equals("")) {
            publicMapper.saveItems(paramsMap);
            return JsonResult.success(saveNum);
        } else {
            return JsonResult.error(400, "订单表新增失败");
        }

    }

    public JsonResult<Integer> update_apply(Map map) {
        Map paramsMap = new HashMap();
        String out_type = "";
        String apply_num = "";
        String return_plandate = "";
        String operator = "";
        String demand_time = "";
        String stock_num = "";
        String apply_id = "";
        String apply_reason = "";
        String flow_id = "";
        String remark = "";
        String currentuser = "";
        String state = "";
        String assets_id = "";
        String use_to = "";
        String for_workshop = "";
        String dept_use = "";
        if (StrUtil.isNotBlank(map.get("out_type") != null ? map.get("out_type").toString() : null)) {
            out_type = map.get("out_type").toString();
        }
        if (StrUtil.isNotBlank(map.get("state") != null ? map.get("state").toString() : null)) {
            state = map.get("state").toString();
        }
        if (StrUtil.isNotBlank(map.get("assets_id") != null ? map.get("assets_id").toString() : null)) {
            assets_id = map.get("assets_id").toString();
        }
        if (StrUtil.isNotBlank(map.get("apply_num") != null ? map.get("apply_num").toString() : null)) {
            apply_num = map.get("apply_num").toString();
        }
        if (StrUtil.isNotBlank(map.get("return_plandate") != null ? map.get("return_plandate").toString() : null)) {
            return_plandate = map.get("return_plandate").toString();
        }
        if (StrUtil.isNotBlank(map.get("stock_num") != null ? map.get("stock_num").toString() : null)) {
            stock_num = map.get("stock_num").toString();
        }
        if (StrUtil.isNotBlank(map.get("demand_time") != null ? map.get("demand_time").toString() : null)) {
            demand_time = map.get("demand_time").toString();
        }

        if (StrUtil.isNotBlank(map.get("apply_id") != null ? map.get("apply_id").toString() : null)) {
            apply_id = map.get("apply_id").toString();
        }
        if (StrUtil.isNotBlank(map.get("apply_reason") != null ? map.get("apply_reason").toString() : null)) {
            apply_reason = map.get("apply_reason").toString();
        }
        if (StrUtil.isNotBlank(map.get("remark") != null ? map.get("remark").toString() : null)) {
            remark = map.get("remark").toString();
        }
        if (StrUtil.isNotBlank(map.get("currentuser") != null ? map.get("currentuser").toString() : null)) {
            currentuser = map.get("currentuser").toString();
        }
        if (StrUtil.isNotBlank(map.get("use_to") != null ? map.get("use_to").toString() : null)) {
            use_to = map.get("use_to").toString();
        }
        if (StrUtil.isNotBlank(map.get("for_workshop") != null ? map.get("for_workshop").toString() : null)) {
            for_workshop = map.get("for_workshop").toString();
        }
        if (StrUtil.isNotBlank(map.get("dept_use") != null ? map.get("dept_use").toString() : null)) {
            dept_use = map.get("dept_use").toString();
        }

        String apply_man = SecurityUtils.getSubject().getPrincipal().toString();
        String sql = " update ws_eng_assets_apply_info"
                + " set "
                + " apply_num=#{apply_num},remark=#{remark},assets_id=#{assets_id},"
                + " demand_time=#{demand_time},stock_num=#{stock_num},apply_reason=#{apply_reason},use_to=#{use_to},for_workshop=#{for_workshop},dept_use=#{dept_use},"
                + "operator_record=concat(operator_record,'==>修改(',#{apply_man},')')"
                + " where apply_id=#{apply_id}";
        paramsMap.put("usqlStr", sql);
        paramsMap.put("apply_num", apply_num);
        paramsMap.put("remark", remark);
        paramsMap.put("assets_id", assets_id);
        paramsMap.put("demand_time", demand_time);
        paramsMap.put("stock_num", stock_num);
        paramsMap.put("apply_reason", apply_reason);
        paramsMap.put("apply_id", apply_id);
        paramsMap.put("apply_man", apply_man);
        paramsMap.put("use_to", use_to);
        paramsMap.put("for_workshop",for_workshop);
        paramsMap.put("dept_use",dept_use);
        return JsonResult.success(publicMapper.updateItems(paramsMap));
    }

    @Override
    public JsonResult<Integer> insertPrintRecord(Map map) {
        String id = (String) map.get("apply_info_order_id");
        Map paramsMap = new HashMap();
        String sql = " insert into ws_eng_assets_print_record " +
                "(apply_info_order_id,createtime) SELECT id ,now() " +
                "from ws_eng_assets_apply_order_info where id = #{id}";
        paramsMap.put("isqlStr", sql);
        paramsMap.put("id", id);
        int creNum = publicMapper.saveItems(paramsMap);
        paramsMap.clear();
        if (creNum == 1) {
            return JsonResult.success();
        } else {
            return JsonResult.error(400, "新增数据失败");
        }
    }

    @Override
    public JsonResult<Integer> del_apply(Map map) {
        String id = (String) map.get("id");
        Map paramsMap = new HashMap();
        String sql = " delete from ws_eng_assets_apply_order_info where id=#{id}";
        paramsMap.put("dsqlStr", sql);
        paramsMap.put("id", id);
        int creNum = publicMapper.delItems(paramsMap);
        paramsMap.clear();
        if (creNum == 1) {
            sql = " delete from ws_eng_assets_apply_info where apply_order_info_id=#{id}";
            paramsMap.put("dsqlStr", sql);
            paramsMap.put("id", id);
            return JsonResult.success(publicMapper.delItems(paramsMap));
        } else {
            return JsonResult.error(400, "删除订单表数据失败");
        }
    }

    @Override
    public JsonResult<Integer> del_applyByOne(Map map) {
        String apply_id = (String) map.get("apply_id");
        String apply_order_info_id = (String) map.get("apply_order_info_id");
        Map paramsMap = new HashMap();
        String sql = "select count(*) as num from ws_eng_assets_apply_info " +
                "where apply_order_info_id = #{apply_order_info_id}";
        paramsMap.put("sqlStr", sql);
        paramsMap.put("apply_order_info_id", apply_order_info_id);
        List<LinkedHashMap<String, Object>> numList = publicMapper.getPublicItems(paramsMap);
        String num = "";
        if (numList.size() > 0) {
            num = numList.get(0).get("num").toString();
        } else {
            return JsonResult.error(ResponseEnum.SERVER_ERROR);
        }
        paramsMap.clear();
        if (num.equals("1")) {
            sql = " delete from ws_eng_assets_apply_info where apply_id=#{apply_id}";
            paramsMap.put("dsqlStr", sql);
            paramsMap.put("apply_id", apply_id);
            publicMapper.delItems(paramsMap);
            paramsMap.clear();
            sql = " delete from ws_eng_assets_apply_order_info where id=#{apply_order_info_id}";
            paramsMap.put("dsqlStr", sql);
            paramsMap.put("apply_order_info_id", apply_order_info_id);
            return JsonResult.success(publicMapper.delItems(paramsMap));
        } else {
            sql = " delete from ws_eng_assets_apply_info where apply_id=#{apply_id}";
            paramsMap.put("dsqlStr", sql);
            paramsMap.put("apply_id", apply_id);
            return JsonResult.success(publicMapper.delItems(paramsMap));
        }
    }

    @Override
    public void exportApplyData(HttpServletResponse response, Map map) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = null;
        try {
            fileName = URLEncoder.encode(map.get("erp_no") + "辅料申请清单", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
        List<ExcelHeader> headerList = new ArrayList<>();
        headerList.add(new ExcelHeader("erp_no", "ERP编号"));
        headerList.add(new ExcelHeader("apply_dept", "申请部门"));
        headerList.add(new ExcelHeader("apply_man", "申请人"));
        ExcelHeader dateHeader = new ExcelHeader("return_plandate", "预计归还");
        dateHeader.setDataType(ExcelHeader.DATE);
        dateHeader.setFormat("yyyy-MM-dd");
        headerList.add(dateHeader);
        ExcelHeader dateApplyHeader = new ExcelHeader("apply_time", "申请时间");
        dateApplyHeader.setDataType(ExcelHeader.DATE);
        dateApplyHeader.setFormat("yyyy-MM-dd");
        headerList.add(dateApplyHeader);
        headerList.add(new ExcelHeader("state", "状态"));
        headerList.add(new ExcelHeader("currentuser", "当前责任人"));
        headerList.add(new ExcelHeader("dept_name", "物资部门"));
        headerList.add(new ExcelHeader("type_main_name", "物资类别"));
        headerList.add(new ExcelHeader("type_dtl_name", "物资名称"));
        headerList.add(new ExcelHeader("type_dtl_no", "物资编号"));
        headerList.add(new ExcelHeader("model", "型号"));
        headerList.add(new ExcelHeader("sizes", "规格"));
        headerList.add(new ExcelHeader("out_type", "出库类型"));
        headerList.add(new ExcelHeader("apply_num", "申请数量"));
        headerList.add(new ExcelHeader("backto_user", "打回人"));
        ExcelHeader dateBackHeader = new ExcelHeader("backto_time", "打回时间");
        dateBackHeader.setDataType(ExcelHeader.DATE);
        dateBackHeader.setFormat("yyyy-MM-dd HH:mm:ss");
        headerList.add(dateBackHeader);
        headerList.add(new ExcelHeader("apply_reason", "申请原因"));
        headerList.add(new ExcelHeader("stock_num", "库存数量"));
        ExcelHeader dateNeedHeader = new ExcelHeader("demand_time", "需求日期");
        dateNeedHeader.setDataType(ExcelHeader.DATE);
        dateNeedHeader.setFormat("yyyy-MM-dd HH:mm:ss");
        headerList.add(dateNeedHeader);

        List<Map<String, Object>> dataList = getApplyInfoList(map);
        OutputStream excelOutputStream = EasyExcelUtils.exportDataToExcel(headerList, dataList);
        try {
            response.getOutputStream().write(((ByteArrayOutputStream) excelOutputStream).toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public JsonResult<Integer> check_apply(Map map) {
        Map paramsMap = new HashMap();
        String apply_order_info_id = map.get("apply_order_info_id").toString();
        String apply_man = map.get("apply_man").toString();
        String urgent_flag = map.get("urgent_flag").toString();
        String currentuser = map.get("currentuser").toString();
        String node_seq = map.get("node_seq").toString();
        String turn_node_seq = map.get("turn_node_seq").toString();
        String state = "";
        String currentuserNew = "";
        String sql = "select b.user_name,b.sequence from ws_eng_assets_operator_config a "
                + "left join ws_eng_assets_operator_config b on a.operator_node = b.operator_node "
                + "and b.sequence>a.sequence "
                + "where a.operator_node=#{node_seq} and a.user_name=#{currentuser} "
                + "order by b.sequence asc limit 1 ";
        paramsMap.put("sqlStr", sql);
        paramsMap.put("node_seq", node_seq);
        paramsMap.put("currentuser", currentuser);
        List<Map<String, Object>> list_config = publicMapper.getPublicItems(paramsMap);
        sql = "select user_name from ws_eng_assets_operator_config where operator_info='打印'";
        paramsMap.put("sqlStr",sql);
        List<Map<String, Object>> printOperatorList = publicMapper.getPublicItems(paramsMap);
        String printOperator = printOperatorList.get(0).get("user_name").toString();
        System.out.println("**************:" + list_config.get(0));
        if (list_config.size() > 0 && list_config.get(0) != null) {
            paramsMap.clear();
            currentuserNew = list_config.get(0).get("user_name").toString();
            sql = " update ws_eng_assets_apply_info set " +
                    "currentuser=#{currentuser_new}," +
                    "operator_record=concat(operator_record,'==>审核(',#{currentuser},now(),')') " +
                    "where apply_order_info_id=#{apply_order_info_id}";
            paramsMap.put("usqlStr", sql);
            paramsMap.put("currentuser_new", currentuserNew);
            paramsMap.put("currentuser", currentuser);
            paramsMap.put("apply_order_info_id", apply_order_info_id);
            int changeNum = publicMapper.updateItems(paramsMap);

            if (urgent_flag.equals("1")) {
                RTX.sendMsg("物资申请消息", apply_man + "已提交申请单---急单！！！", currentuserNew);
                RTX.sendMsg("物资申请消息", apply_man + "已提交申请单---急单！！！", printOperator);
                return JsonResult.success(changeNum);
            } else {
                RTX.sendMsg("物资申请消息", apply_man + "已提交申请单", currentuserNew);
                RTX.sendMsg("物资申请消息", apply_man + "已提交申请单---急单！！！", printOperator);
                return JsonResult.success(changeNum);
            }
        } else {
            paramsMap.clear();
            sql = "select a.node_seq,a.turn_node_seq,a.node_name,b.user_name,b.sequence from " +
                    "ws_eng_assets_apply_flow a " +
                    "join ws_eng_assets_operator_config b " +
                    "on a.node_name=b.operator_info and a.node_seq=b.operator_node " +
                    "where a.model_name='工程物资申请' and a.node_seq=#{node_seq} " +
                    "order by b.sequence limit 1";
            paramsMap.put("sqlStr", sql);
            paramsMap.put("node_seq", turn_node_seq);
            List<Map<String, Object>> list_config2 = publicMapper.getPublicItems(paramsMap);
            node_seq = list_config2.get(0).get("node_seq").toString();
            turn_node_seq = list_config2.get(0).get("turn_node_seq").toString();
            currentuserNew = list_config2.get(0).get("user_name").toString();
            state = list_config2.get(0).get("node_name").toString();

            sql = " update ws_eng_assets_apply_order_info "
                    + " set state=#{state},remark=''"
                    + " where id=#{apply_order_info_id}";
            paramsMap.put("usqlStr", sql);
            paramsMap.put("state", state);
            paramsMap.put("apply_order_info_id", apply_order_info_id);
            publicMapper.updateItems(paramsMap);
            paramsMap.clear();
            sql = " update ws_eng_assets_apply_info " +
                    "set state=#{state},currentuser=#{currentuser_new}," +
                    "operator_record=concat(operator_record,'==>审核(',#{currentuser},now(),')')," +
                    "node_seq=#{node_seq},turn_node_seq=#{turn_node_seq} " +
                    "where apply_order_info_id=#{apply_order_info_id}";
            paramsMap.put("usqlStr", sql);
            paramsMap.put("state", state);
            paramsMap.put("currentuser_new", currentuserNew);
            paramsMap.put("currentuser", currentuser);
            paramsMap.put("node_seq", node_seq);
            paramsMap.put("turn_node_seq", turn_node_seq);
            paramsMap.put("apply_order_info_id", apply_order_info_id);
            int changeNum = publicMapper.updateItems(paramsMap);
            if (urgent_flag.equals("1")) {
                RTX.sendMsg("物资申请消息", apply_man + "已提交申请单---急单！！！", currentuser);
                RTX.sendMsg("物资申请消息", apply_man + "已提交申请单---急单！！！", printOperator);
                return JsonResult.success(changeNum);
            } else {
                RTX.sendMsg("物资申请消息", apply_man + "已提交申请单", currentuser);
                RTX.sendMsg("物资申请消息", apply_man + "已提交申请单---急单！！！", printOperator);
                return JsonResult.success(changeNum);
            }
//            return JsonResult.error(ResponseEnum.SERVER_ERROR);
        }
    }

    public JsonResult<Integer> back_apply(HttpServletRequest request, Map map) {
        Map paramsMap = new HashMap();
        String apply_order_info_id = map.get("apply_order_info_id").toString();
        String apply_man = map.get("apply_man").toString();
        String urgent_flag = map.get("urgent_flag").toString();
        String currentuser = map.get("currentuser").toString();
        String node_seq = map.get("node_seq").toString();
        String turn_node_seq = map.get("turn_node_seq").toString();
        String rs = map.get("rs").toString();
        String state = "打回";
        String sql = " update ws_eng_assets_apply_order_info "
                + " set state=#{state},remark=#{remark}"
                + " where id=#{apply_order_info_id}";
        paramsMap.put("usqlStr", sql);
        paramsMap.put("state", state);
        paramsMap.put("remark", rs + "(打回人：" + currentuser + ")");
        paramsMap.put("apply_order_info_id", apply_order_info_id);
        publicMapper.updateItems(paramsMap);
        paramsMap.clear();
        sql = " update ws_eng_assets_apply_info " +
                "set state=#{state}," +
                "operator_record=concat(operator_record,'==>打回:" + rs + "(',#{currentuser},now(),')')," +
                "node_seq=#{node_seq},turn_node_seq=#{turn_node_seq},currentuser=#{apply_man}," +
                "refusereason=#{refusereason},refusereason_user=#{refusereason_user},refusereason_time=now()" +
                "where apply_order_info_id=#{apply_order_info_id}";
        paramsMap.put("usqlStr", sql);
        paramsMap.put("state", state);
        paramsMap.put("currentuser", currentuser);
        paramsMap.put("node_seq", "-1");
        paramsMap.put("turn_node_seq", "1");
        paramsMap.put("apply_man", apply_man);
        paramsMap.put("refusereason", rs);
        paramsMap.put("refusereason_user", currentuser);
        paramsMap.put("apply_order_info_id", apply_order_info_id);
        int changeNum = publicMapper.updateItems(paramsMap);
        sql = "select user_name from ws_eng_assets_operator_config where operator_info='打印'";
        paramsMap.put("sqlStr",sql);
        List<Map<String, Object>> printOperatorList = publicMapper.getPublicItems(paramsMap);
        String printOperator = printOperatorList.get(0).get("user_name").toString();
        RTX.sendMsg("物资申请消息", "你提交的订单已被打回！原因：" + rs + "(打回人：" + currentuser + ")", apply_man);
        RTX.sendMsg("物资申请消息", "你提交的订单已被打回！原因：" + rs + "(打回人：" + currentuser + ")", printOperator);
        return JsonResult.success(changeNum);
    }

    public JsonResult<Integer> issue_apply(HttpServletRequest request, Map map) {
        Map paramsMap = new HashMap();
        String apply_order_info_id = map.get("apply_order_info_id").toString();
        String apply_man = map.get("apply_man").toString();
        String urgent_flag = map.get("urgent_flag").toString();
        String currentuser = map.get("currentuser").toString();
        String node_seq = map.get("node_seq").toString();
        String turn_node_seq = map.get("turn_node_seq").toString();
        String state = "";
        String sql = " update ws_eng_assets_apply_order_info "
                + " set state=#{state}"
                + " where id=#{apply_order_info_id}";
        paramsMap.put("usqlStr", sql);
        paramsMap.put("state", "已打印");
        paramsMap.put("apply_order_info_id", apply_order_info_id);
        publicMapper.updateItems(paramsMap);
        paramsMap.clear();
        sql = " update ws_eng_assets_apply_info " +
                "set state=#{state}," +
                "operator_record=concat(operator_record,'==>打印(',#{currentuser},now(),')')," +
                "node_seq=#{node_seq},turn_node_seq=#{turn_node_seq} " +
                "where apply_order_info_id=#{apply_order_info_id}";
        paramsMap.put("usqlStr", sql);
        paramsMap.put("state", "已打印");
        paramsMap.put("currentuser", currentuser);
        paramsMap.put("node_seq", turn_node_seq);
        paramsMap.put("turn_node_seq", "999");
        paramsMap.put("apply_order_info_id", apply_order_info_id);
        int changeNum = publicMapper.updateItems(paramsMap);
        RTX.sendMsg("物资申请消息", "你提交的订单已打印", apply_man);
        return JsonResult.success(changeNum);

//            return JsonResult.error(ResponseEnum.SERVER_ERROR);


//        //获取当前操作用户
//        if (request.getSession().getAttribute(Const.SESSION_USER) != null) {
//            UserResponse user = (UserResponse) request.getSession().getAttribute(Const.SESSION_USER);
//            username = user != null ? user.getUsername() : null;
//        }
//        sqltmp = "operator_record=concat(operator_record,'==>打印(',#{operator},')')";
//        String sql = " update ws_eng_assets_apply_info set currentuser=apply_man,"
//                + " node_seq='3',turn_node_seq='999',state=#{state}, "
//                + sqltmp + " where erp_no=#{erp_no}";
//        paramsMap.put("usqlStr", sql);
//        paramsMap.put("erp_no", erp_no);
//        paramsMap.put("operator", username);
//        paramsMap.put("state", "已打印");
//        int updateNum = publicMapper.updateItems(paramsMap);
//        if (updateNum > 0) {
//            sendInfo = "您的物资申请已打印";
//            RTX.sendMsg("物资申请消息", sendInfo, apply_man);
//            return JsonResult.success(updateNum);
//        } else {
//            return JsonResult.error(ResponseEnum.SERVER_ERROR);
//        }
    }


    @Override
    public JsonResult<Integer> save_flow(Map map) {
        Map paramsMap = new HashMap();
        String model_name = map.get("model_name").toString();
        String node_name = map.get("node_name").toString();
        String node_seq = map.get("node_seq").toString();
        String turn_node_seq = map.get("turn_node_seq").toString();
        String serial_flag = map.get("serial_flag").toString();
        String sql = " insert into ws_eng_assets_apply_flow(model_name,node_seq,node_name,turn_node_seq,"
                + " serial_flag) "
                + " select #{model_name},#{node_seq},#{node_name},#{turn_node_seq},#{serial_flag}";
        paramsMap.put("isqlStr", sql);
        paramsMap.put("model_name", model_name);
        paramsMap.put("node_seq", node_seq);
        paramsMap.put("node_name", node_name);
        paramsMap.put("turn_node_seq", turn_node_seq);
        paramsMap.put("serial_flag", serial_flag);

        return JsonResult.success(publicMapper.saveItems(paramsMap));
    }

    @Override
    public JsonResult<Integer> update_flow(Map map) {
        Map paramsMap = new HashMap();
        String model_name = map.get("model_name").toString();
        String node_name = map.get("node_name").toString();
        String node_seq = map.get("node_seq").toString();
        String turn_node_seq = map.get("turn_node_seq").toString();
        String serial_flag = map.get("serial_flag").toString();
        String id = map.get("id").toString();
        String sql = " update ws_eng_assets_apply_flow "
                + " set model_name=#{model_name},node_name=#{node_name},node_seq=#{node_seq},"
                + " turn_node_seq=#{turn_node_seq},serial_flag=#{serial_flag} "
                + " where id=#{id}";
        paramsMap.put("usqlStr", sql);
        paramsMap.put("model_name", model_name);
        paramsMap.put("node_name", node_name);
        paramsMap.put("turn_node_seq", turn_node_seq);
        paramsMap.put("serial_flag", serial_flag);
        paramsMap.put("node_seq", node_seq);
        paramsMap.put("id", id);

        return JsonResult.success(publicMapper.updateItems(paramsMap));
    }

    @Override
    public JsonResult<Integer> submitApply(Map map) {
        Map paramsMap = new HashMap();
        String apply_order_info_id = map.get("apply_order_info_id").toString();
        String apply_man = map.get("apply_man").toString();
        String urgent_flag = map.get("urgent_flag").toString();
        String node_seq = "";
        String turn_node_seq = "";
        String currentuser = "";
        String sql = "select a.node_seq,a.turn_node_seq,b.user_name,b.sequence from " +
                "ws_eng_assets_apply_flow a " +
                "join ws_eng_assets_operator_config b " +
                "on a.node_name=b.operator_info and a.node_seq=b.operator_node " +
                "where a.model_name='工程物资申请' and a.node_name='审核' " +
                "order by b.sequence limit 1";
        paramsMap.put("sqlStr", sql);
        List<Map<String, Object>> list_config = publicMapper.getPublicItems(paramsMap);

        node_seq = list_config.get(0).get("node_seq").toString();
        turn_node_seq = list_config.get(0).get("turn_node_seq").toString();
        currentuser = list_config.get(0).get("user_name").toString();

        sql = " update ws_eng_assets_apply_order_info "
                + " set state='审核'"
                + " where id=#{apply_order_info_id}";
        paramsMap.put("usqlStr", sql);
        paramsMap.put("apply_order_info_id", apply_order_info_id);
        publicMapper.updateItems(paramsMap);
        paramsMap.clear();
        sql = " update ws_eng_assets_apply_info " +
                "set state='审核',currentuser=#{currentuser}," +
                "operator_record=concat(operator_record,'==>提交(',#{apply_man},now(),')')," +
                "node_seq=#{node_seq},turn_node_seq=#{turn_node_seq} " +
                "where apply_order_info_id=#{apply_order_info_id}";
        paramsMap.put("usqlStr", sql);
        paramsMap.put("currentuser", currentuser);
        paramsMap.put("apply_man", apply_man);
        paramsMap.put("node_seq", node_seq);
        paramsMap.put("turn_node_seq", turn_node_seq);
        paramsMap.put("apply_order_info_id", apply_order_info_id);
        int changeNum = publicMapper.updateItems(paramsMap);
        sql = "select user_name from ws_eng_assets_operator_config where operator_info='打印'";
        paramsMap.put("sqlStr",sql);
        List<Map<String, Object>> printOperatorList = publicMapper.getPublicItems(paramsMap);
        String printOperator = printOperatorList.get(0).get("user_name").toString();
        if (urgent_flag.equals("1")) {
            RTX.sendMsg("物资申请消息", apply_man + "已提交申请单---急单！！！", currentuser);
            RTX.sendMsg("物资申请消息", apply_man + "已提交申请单---急单！！！", printOperator);
            return JsonResult.success(changeNum);
        } else {
            RTX.sendMsg("物资申请消息", apply_man + "已提交申请单", currentuser);
            RTX.sendMsg("物资申请消息", apply_man + "已提交申请单", printOperator);
            return JsonResult.success(changeNum);
        }
    }

    @Override
    public JsonResult<Integer> del_flow(Map map) {
        Integer id = Integer.parseInt(map.get("id").toString());
        Map paramsMap = new HashMap();
        String sql = " delete from ws_eng_assets_apply_flow where id=#{id}";
        paramsMap.put("dsqlStr", sql);
        paramsMap.put("id", id);
        return JsonResult.success(publicMapper.delItems(paramsMap));
    }

    @Override
    public List<Map<String, Object>> getApplyNode(Map map) {
        String sql = " select * from ws_eng_assets_apply_flow where 1=1";
        Map paramsMap = new HashMap();
        String model_name = "";
        if (StrUtil.isNotBlank(map.get("model_name") != null ? map.get("model_name").toString() : null)) {
            sql += " and model_name=#{model_name}";
            model_name = map.get("model_name").toString();
            paramsMap.put("model_name", model_name);
        }
        paramsMap.put("sqlStr", sql);
        return publicMapper.getPublicItems(paramsMap);
    }

    @Override
    public List<Map<String, Object>> getApplyList(Map map) {
        String erp_no = "";
        String sqltmp = "";
        String apply_man = "";
        String username = "";
        String state = "";
        String conDept = "";
        String conProductNum = "";
        String cusSentToName = "";
        String currentuser = "";
        Map paramsMap = new HashMap();
        Integer page = Integer.parseInt((map.get("page") != null ? map.get("page").toString() : "0"));
        Integer rows = Integer.parseInt((map.get("rows") != null ? map.get("rows").toString() : "0"));

        if (StrUtil.isNotBlank(map.get("erp_no") != null ? map.get("erp_no").toString() : null)) {
            sqltmp += " and a.erp_no=#{erp_no}";
            erp_no = map.get("erp_no").toString();
            paramsMap.put("erp_no", erp_no);
        }
        if (StrUtil.isNotBlank(map.get("apply_man") != null ? map.get("apply_man").toString() : null)) {
            sqltmp += " and a.apply_name=#{apply_man}";
            apply_man = map.get("apply_man").toString();
            paramsMap.put("apply_man", apply_man);
        }
        if (StrUtil.isNotBlank(map.get("conDept") != null ? map.get("conDept").toString() : null)) {
            sqltmp += " and e.conDept=#{conDept}";
            conDept = map.get("conDept").toString();
            paramsMap.put("conDept", conDept);
        }
        if (StrUtil.isNotBlank(map.get("conProductNum") != null ? map.get("conProductNum").toString() : null)) {
            sqltmp += " and e.conProductNum=#{conProductNum}";
            conProductNum = map.get("conProductNum").toString();
            paramsMap.put("conProductNum", conProductNum);
        }
        if (StrUtil.isNotBlank(map.get("cusSentToName") != null ? map.get("cusSentToName").toString() : null)) {
            sqltmp += " and e.cusSentToName=#{cusSentToName}";
            cusSentToName = map.get("cusSentToName").toString();
            paramsMap.put("cusSentToName", cusSentToName);
        }
        if (StrUtil.isNotBlank(map.get("currentuser") != null ? map.get("currentuser").toString() : null)) {
            sqltmp += " and b.currentuser=#{currentuser}";
            currentuser = map.get("currentuser").toString();
            paramsMap.put("currentuser", currentuser);
        }

        if (StrUtil.isNotBlank(map.get("state") != null ? map.get("state").toString() : null)) {
            sqltmp += " and a.state=#{state}";
            state = map.get("state").toString();
            paramsMap.put("state", state);
        }

        String sql = " select count(*) as totals from " +
                "(select a.id from ws_eng_assets_apply_order_info a " +
                "join ws_eng_assets_apply_info b " +
                "on a.id = b.apply_order_info_id "
                + "join ws_eng_assets_type_dtl c "
                + "on b.assets_id=c.type_dtl_id "
                + "join ws_eng_assets_type_main d on c.type_main_id=d.type_main_id "
                + "join (select conErpNo from uws_produceorder_confirmation_query where conErpNo is not null group by conErpNo) e "
                + "on a.erp_no = e.conErpNo " +
                "left join (select *,count(apply_info_order_id) as print_count from ws_eng_assets_print_record group by apply_info_order_id order by createtime) f " +
                "on f.apply_info_order_id=a.id "
                + " where 1=1 " + sqltmp + " group by a.id order by a.urgent_flag desc,a.apply_time) f ";
        paramsMap.put("sqlStr", sql);
        int totals = 0;
        List<Map<String, Object>> list_count = publicMapper.getPublicItems(paramsMap);
        if (list_count.size() > 0) {
            totals = Integer.parseInt(list_count.get(0).get("totals").toString());
        } else {
            System.out.println("****************:" + totals);
        }


        sql = " select a.id,a.erp_no,a.apply_dept,a.apply_name,a.apply_time,a.state,a.urgent_flag,a.remark,"
                + "b.currentuser,b.node_seq,b.turn_node_seq,b.apply_time,b.apply_man,f.print_count,f.createtime as print_time,"
                + "e.conErpNo,e.conProductNum,e.conDept,e.sdOrderAmount,e.cusSentToName,b.for_workshop," + totals + " as totals from " +
                "ws_eng_assets_apply_order_info a " +
                "join ws_eng_assets_apply_info b " +
                "on a.id = b.apply_order_info_id "
                + "join ws_eng_assets_type_dtl c "
                + "on b.assets_id=c.type_dtl_id "
                + "join ws_eng_assets_type_main d on c.type_main_id=d.type_main_id "
                + "join (select conErpNo,conProductNum,conDept,sdOrderAmount,cusSentToName from uws_produceorder_confirmation_query where conErpNo is not null group by conErpNo) e "
                + "on a.erp_no = e.conErpNo " +
                "left join (select *,count(apply_info_order_id) as print_count from " +
                "(select * from ws_eng_assets_print_record order by createtime desc) f1 group by apply_info_order_id ) f " +
                "on f.apply_info_order_id=a.id "
                + " where 1=1 " + sqltmp + " group by a.id order by a.apply_time desc ";
        System.out.println("sql:"+sql);
        if (page != 0 && rows != 0) {
            sql += " limit #{startIndex},#{rows} ";
            paramsMap.put("startIndex", (page - 1) * rows);
            paramsMap.put("rows", rows);
        }
        paramsMap.put("sqlStr", sql);
        return publicMapper.getPublicItems(paramsMap);
    }

    @Override
    public List<Map<String, Object>> getApplyInfoList(Map map) {
        String erp_no = "";
        String sqltmp = "";
        String apply_man = "";
        String apply_order_info_id = "";
        Map paramsMap = new HashMap();
        Integer page = Integer.parseInt((map.get("page") != null ? map.get("page").toString() : "0"));
        Integer rows = Integer.parseInt((map.get("rows") != null ? map.get("rows").toString() : "0"));

        if (StrUtil.isNotBlank(map.get("erp_no") != null ? map.get("erp_no").toString() : null)) {
            sqltmp += " and a.erp_no=#{erp_no}";
            erp_no = map.get("erp_no").toString();
            paramsMap.put("erp_no", erp_no);
        }
        if (StrUtil.isNotBlank(map.get("apply_man") != null ? map.get("apply_man").toString() : null)) {
            sqltmp += " and a.apply_man=#{apply_man}";
            apply_man = map.get("apply_man").toString();
            paramsMap.put("apply_man", apply_man);
        }
        if (StrUtil.isNotBlank(map.get("apply_order_info_id") != null ? map.get("apply_order_info_id").toString() : null)) {
            sqltmp += " and a.apply_order_info_id=#{apply_order_info_id}";
            apply_order_info_id = map.get("apply_order_info_id").toString();
            paramsMap.put("apply_order_info_id", apply_order_info_id);
        }
        String sql = " select count(*) as totals from ws_eng_assets_apply_info a where 1=1 " + sqltmp;
        paramsMap.put("sqlStr", sql);
        int totals = 0;
        List<Map<String, Object>> list_count = publicMapper.getPublicItems(paramsMap);
        if (list_count.size() > 0) {
            totals = Integer.parseInt(list_count.get(0).get("totals").toString());
        }

        sql = " select a.erp_no,a.apply_order_info_id,a.apply_dept,a.apply_man,a.return_plandate,a.apply_time,a.state,a.currentuser, "
                + "a.state,a.currentuser,b.dept_name,b.type_main_name,c.type_dtl_name,c.type_dtl_no,c.model,c.sizes,a.out_type, "
                + "a.apply_num,a.backto_user,a.backto_time,a.apply_reason,a.stock_num,a.demand_time,a.remark,a.operator_record,"
                + "a.besureout_user,a.besureout_time,a.refusereason,a.refusereason_user,a.refusereason_time, "
                + "a.node_seq,a.turn_node_seq,a.assets_id,c.k3_code,c.brand_name,a.apply_id,a.dept_use,"
                + "d.conErpNo,d.conProductNum,d.conDept,d.sdOrderAmount,d.cusSentToName,e.urgent_flag,a.use_to,a.generic_flag,c.validation_flag,a.for_workshop," + totals + " as totals " +
                " from ws_eng_assets_apply_info a "
                + "left join ws_eng_assets_type_dtl c "
                + "on a.assets_id=c.type_dtl_id "
                + "left join "
                + "ws_eng_assets_type_main b on b.type_main_id=c.type_main_id "
                + "left join uws_produceorder_confirmation_query d "
                + "on a.erp_no = d.conErpNo "
                + "join ws_eng_assets_apply_order_info e "
                + "on a.apply_order_info_id = e.id "
                + "where 1=1 " + sqltmp + " group by a.apply_id order by a.apply_time";
        if (page != 0 && rows != 0) {
            sql += " limit #{startIndex},#{rows} ";
            paramsMap.put("startIndex", (page - 1) * rows);
            paramsMap.put("rows", rows);
        }
        paramsMap.put("sqlStr", sql);
        return publicMapper.getPublicItems(paramsMap);
    }

    @Override
    public List<Map<String, Object>> getApplyInfoByScreen(Map map) {
        String type_dtl_name = "";
        String sizes = "";
        String sqltmp = "";
        String apply_order_info_id = map.get("apply_order_info_id").toString();
        Map paramsMap = new HashMap();
        Integer page = Integer.parseInt((map.get("page") != null ? map.get("page").toString() : "0"));
        Integer rows = Integer.parseInt((map.get("rows") != null ? map.get("rows").toString() : "0"));

        if (StrUtil.isNotBlank(map.get("type_dtl_name") != null ? map.get("type_dtl_name").toString() : null)) {
            sqltmp += " and c.type_dtl_name like #{type_dtl_name}";
            type_dtl_name = map.get("type_dtl_name").toString();
            paramsMap.put("type_dtl_name", "%" + type_dtl_name + "%");
        }
        if (StrUtil.isNotBlank(map.get("sizes") != null ? map.get("sizes").toString() : null)) {
            sqltmp += " and c.sizes like #{sizes}";
            sizes = map.get("sizes").toString();
            paramsMap.put("sizes", "%" + sizes + "%");
        }
        String sql = " select count(*) as totals from ws_eng_assets_apply_info a "
                + "left join ws_eng_assets_type_dtl c "
                + "on a.assets_id=c.type_dtl_id "
                + "left join "
                + "ws_eng_assets_type_main b on b.type_main_id=c.type_main_id "
                + "left join uws_produceorder_confirmation_query d "
                + "on a.erp_no = d.conErpNo " +
                " join ws_eng_assets_apply_order_info e " +
                " on e.id = a.apply_order_info_id "
                + "where a.apply_order_info_id=#{apply_order_info_id}  " + sqltmp;
        paramsMap.put("sqlStr", sql);
        paramsMap.put("apply_order_info_id", apply_order_info_id);
        System.out.println("******************:" + sql);
        int totals = 0;
        List<Map<String, Object>> list_count = publicMapper.getPublicItems(paramsMap);
        if (list_count.size() > 0) {
            totals = Integer.parseInt(list_count.get(0).get("totals").toString());
        }
        //增加a.apply_order_info_id
        sql = " select a.erp_no,a.apply_order_info_id,a.apply_dept,a.apply_man,a.return_plandate,a.apply_time,a.state,a.currentuser, "
                + "a.state,a.currentuser,b.dept_name,b.type_main_name,c.type_dtl_name,c.type_dtl_no,c.model,c.sizes,a.out_type, "
                + "a.apply_num,a.backto_user,a.backto_time,a.apply_reason,a.stock_num,a.demand_time,a.remark,a.operator_record,"
                + "a.besureout_user,a.besureout_time,a.refusereason,a.refusereason_user,a.refusereason_time, "
                + "a.node_seq,a.turn_node_seq,a.assets_id,c.use_to,c.k3_code,c.brand_name,a.apply_id,"
                + "d.conErpNo,d.conProductNum,d.conDept,d.sdOrderAmount,d.cusSentToName,a.for_workshop," + totals + " as totals from ws_eng_assets_apply_info a "
                + "left join ws_eng_assets_type_dtl c "
                + "on a.assets_id=c.type_dtl_id "
                + "left join "
                + "ws_eng_assets_type_main b on b.type_main_id=c.type_main_id "
                + "left join uws_produceorder_confirmation_query d "
                + "on a.erp_no = d.conErpNo " +
                "join ws_eng_assets_apply_order_info e " +
                "on e.id = a.apply_order_info_id "
                + "where a.apply_order_info_id=#{apply_order_info_id}  " + sqltmp;
        if (page != 0 && rows != 0) {
            sql += " limit #{startIndex},#{rows} ";
            paramsMap.put("startIndex", (page - 1) * rows);
            paramsMap.put("rows", rows);
        }
        paramsMap.put("sqlStr", sql);
        paramsMap.put("apply_order_info_id", apply_order_info_id);
        return publicMapper.getPublicItems(paramsMap);
    }


    @Override
    public List<Map<String, Object>> getApplyById(String id) {
        Map paramsMap = new HashMap();
        String sql = " select a.*,b.*,c.conErpNo,c.conProductNum,c.conDept,c.sdOrderAmount,c.cusSentToName  from ws_eng_assets_apply_info a "
                + "left join ws_eng_assets_type_dtl b "
                + "on a.assets_id=b.type_dtl_id "
                + "left join uws_produceorder_confirmation_query c "
                + "on a.erp_no = c.conErpNo "
                + "where a.apply_id=#{apply_id}";
        paramsMap.put("sqlStr", sql);
        paramsMap.put("apply_id", id);
        return publicMapper.getPublicItems(paramsMap);
    }

    @Override
    public List<Map<String, Object>> getOperator(Map map) {
        String operator_node = "";
        Map paramsMap = new HashMap();
        String sql = " select * from ws_eng_assets_operator_config where 1=1 ";
        if (StrUtil.isNotBlank(map.get("node_seq") != null ? map.get("node_seq").toString() : null)) {
            sql += " and operator_node=#{operator_node} order by sequence asc ";
            operator_node = map.get("node_seq").toString();
            paramsMap.put("operator_node", operator_node);
        }
        paramsMap.put("sqlStr", sql);
        return publicMapper.getPublicItems(paramsMap);
    }

    @Override
    public List<Map<String, Object>> getErpNo(Map map) {

        Map paramsMap = new HashMap();
        String conErpNo = "";
        String sql = " select conErpNo,cusSentToName,conProductNum,sdOrderAmount,conDept from uws_produceorder_confirmation_query where conErpNo is not null ";
        if (map != null) {
            if (StrUtil.isNotBlank(map.get("conErpNo") != null ? map.get("conErpNo").toString() : null)) {
                sql += " and conErpNo=#{conErpNo} ";
                conErpNo = map.get("conErpNo").toString();
                paramsMap.put("conErpNo", conErpNo);
            }
        }
        paramsMap.put("sqlStr", sql);
        return publicMapper.getPublicItems(paramsMap);
    }

    @Override
    public List<Map<String, Object>> getErpInfoForPrint(Map map) {

        Map paramsMap = new HashMap();
        String apply_order_info_id = "";
        String sql = "select a.conErpNo,a.cusSentToName,a.conProductNum,a.sdOrderAmount,a.conDept, " +
                " c.type_dtl_name,b.apply_num,c.sizes,b.apply_reason,b.demand_time,b.stock_num,b.erp_no," +
                " b.apply_man,b.remark,d.urgent_flag,c.type_dtl_no,b.use_to,d.id as apply_info_order_id,c.unit,b.for_workshop,b.dept_use " +
                " from uws_produceorder_confirmation_query a" +
                " join ws_eng_assets_apply_info b " +
                " on a.conErpNo=b.erp_no " +
                " join ws_eng_assets_apply_order_info d "+
                " on b.apply_order_info_id=d.id "+
                " join ws_eng_assets_type_dtl c " +
                " on b.assets_id = c.type_dtl_id" +
                " where 1=1 ";
        if (map != null) {
            if (StrUtil.isNotBlank(map.get("apply_order_info_id") != null ? map.get("apply_order_info_id").toString() : null)) {
                sql += " and b.apply_order_info_id=#{apply_order_info_id} group by b.apply_id";
                apply_order_info_id = map.get("apply_order_info_id").toString();
                paramsMap.put("apply_order_info_id", apply_order_info_id);
            }
        }
        paramsMap.put("sqlStr", sql);
        return publicMapper.getPublicItems(paramsMap);
    }

    @Override
    public JsonResult<Integer> get_UrgentFlag(Map map) {
        Map paramsMap = new HashMap();
        String id = map.get("apply_order_info_id").toString();
        String sql = "select urgent_flag from ws_eng_assets_apply_order_info where id = #{apply_order_info_id}";
        paramsMap.put("sqlStr", sql);
        paramsMap.put("apply_order_info_id", id);
        List<LinkedHashMap<String, Object>> urgent_list = publicMapper.getPublicItems(paramsMap);
        if (urgent_list.size() > 0) {
            String flag = urgent_list.get(0).get("urgent_flag").toString();
            int flag_int = Integer.parseInt(flag);
            return JsonResult.success(flag_int);
        } else {
            return JsonResult.error(ResponseEnum.SERVER_ERROR);
        }
    }

    @Override
    public List<Map<String, Object>> getStockinNum(Map map) {
        Map paramsMap = new HashMap();
        String type_dtl_no = "";
        String sql = " select stockin_num from ws_eng_assets_delivery_record " +
                "where assets_id = " +
                "(select type_dtl_id from ws_eng_assets_type_dtl where type_dtl_no = #{type_dtl_no})" +
                " order by id desc limit 1";
        if (map != null) {
            if (StrUtil.isNotBlank(map.get("type_dtl_no") != null ? map.get("type_dtl_no").toString() : null)) {
                type_dtl_no = map.get("type_dtl_no").toString();
                paramsMap.put("type_dtl_no", type_dtl_no);
            }
        }
        paramsMap.put("sqlStr", sql);
        List<Map<String, Object>> listInfo = publicMapper.getPublicItems(paramsMap);
        System.out.println("----listInfo.size():"+listInfo.size());
        if(listInfo.size()==0){
            sql = "select sum(num) as stockin_num from ws_eng_assets_stockin_record a " +
                    "join ws_eng_assets_type_dtl b on a.type_dtl_id=b.type_dtl_id " +
                    "where b.type_dtl_no=#{type_dtl_no}";
            paramsMap.put("sqlStr", sql);
            listInfo = publicMapper.getPublicItems(paramsMap);
        }
        return listInfo;
    }

    public List<Map<String, Object>> getAssets(Map map) {

        String type_main_id = "";
        String typeNo = "";
        String assetsName = "";
        String assetsSizes = "";
        String sqlmp = "";
        int totals = 0 ;
        Map paramsMap = new HashMap();

//        if (StrUtil.isNotBlank(map.get("type_main_id") != null ? map.get("type_main_id").toString() : null)) {
//            sql += " and type_main_id=#{type_main_id} ";
//            type_main_id = map.get("type_main_id").toString();
//            paramsMap.put("type_main_id", type_main_id);
//        }
        if (StrUtil.isNotBlank(map.get("sizes") != null ? map.get("sizes").toString() : null)) {
            sqlmp += " and a.sizes like #{sizes} ";
            assetsSizes = map.get("sizes").toString();
            paramsMap.put("sizes", "%" + assetsSizes + "%");
        }
        if (StrUtil.isNotBlank(map.get("type_dtl_no") != null ? map.get("type_dtl_no").toString() : null)) {
            sqlmp += " and a.type_dtl_no=#{typeNo} ";
            typeNo = map.get("type_dtl_no").toString();
            paramsMap.put("typeNo", typeNo);
        }
        if (StrUtil.isNotBlank(map.get("type_dtl_name") != null ? map.get("type_dtl_name").toString() : null)) {
            sqlmp += " and a.type_dtl_name like #{assetsname} ";
            assetsName = map.get("type_dtl_name").toString();
            paramsMap.put("assetsname","%"+ assetsName +"%");
        }
        String sql = " select count(*) as totals from ws_eng_assets_type_dtl a "
                + " left join ws_eng_assets_type_main b on a.type_main_id = b.type_main_id "
                + " where 1=1 "+sqlmp;
        paramsMap.put("sqlStr", sql);
        List<Map<String, Object>> list_count = publicMapper.getPublicItems(paramsMap);
        if (list_count.size() > 0) {
            totals = Integer.parseInt(list_count.get(0).get("totals").toString());
        }

        sql = " select a.*,b.*,"+ totals +" as totals from ws_eng_assets_type_dtl a "
                + " left join ws_eng_assets_type_main b on a.type_main_id = b.type_main_id "
                + " where 1=1 "+sqlmp;

        Integer page = Integer.parseInt((map.get("page") != null ? map.get("page").toString() : "0"));
        Integer rows = Integer.parseInt((map.get("rows") != null ? map.get("rows").toString() : "0"));
        if (page != 0 && rows != 0) {
            sql += " limit #{startIndex},#{rows} ";
            paramsMap.put("startIndex", (page - 1) * rows);
            paramsMap.put("rows", rows);
        }
        paramsMap.put("sqlStr", sql);
        return publicMapper.getPublicItems(paramsMap);
    }

    public JsonResult<Boolean> judgePermission(String id, String username, String state) {
        String sql = "select * from ws_eng_assets_apply_order_info a " +
                "join ws_eng_assets_apply_info b on a.id = b.apply_order_info_id " +
                "join ws_eng_assets_operator_config c on b.currentuser = c.user_name " +
                "where a.apply_name=#{user_name} and a.id=#{id}";
        Map paramsMap = new HashMap();
        paramsMap.put("user_name", username);
        paramsMap.put("id", id);
        paramsMap.put("sqlStr", sql);
        List<Map<String, Object>> list_name = publicMapper.getPublicItems(paramsMap);
        if (list_name.size() != 0) {

            return JsonResult.success(true);
        }
        return JsonResult.success(false);
    }

    @Override
    public JsonResult<Boolean> cheackBefore(Map map) {
        Map paramsMap = new HashMap();
        String operator = "";
        String flow_id = "";
        String currentuser = "";
        String state = "";
        String username = "";
        String apply_man = "";
        if (StrUtil.isNotBlank(map.get("currentuser") != null ? map.get("currentuser").toString() : null)) {
            currentuser = map.get("currentuser").toString();
        }
        if (StrUtil.isNotBlank(map.get("state") != null ? map.get("state").toString() : null)) {
            state = map.get("state").toString();
        }
        if (StrUtil.isNotBlank(map.get("username") != null ? map.get("username").toString() : null)) {
            username = map.get("username").toString();
        }
        if (StrUtil.isNotBlank(map.get("apply_man") != null ? map.get("apply_man").toString() : null)) {
            apply_man = map.get("apply_man").toString();
        }

        String sql = " select id,node_seq,turn_node_seq,node_name from ws_eng_assets_apply_flow "
                + " where model_name=#{model_name} order by id asc limit 1 ";
        paramsMap.put("model_name", "工程物资申请");
        paramsMap.put("sqlStr", sql);
        List<LinkedHashMap<String, Object>> nodeList = publicMapper.getPublicItems(paramsMap);

        flow_id = nodeList.get(0).get("node_seq").toString();

        paramsMap.clear();
        sql = " select a.user_name,a.sequence from ws_eng_assets_operator_config a "
                + " where a.operator_node=#{flow_id} "
                + " order by a.sequence asc limit 1 ";
        paramsMap.put("sqlStr", sql);
        paramsMap.put("flow_id", flow_id);
        List<LinkedHashMap<String, Object>> operatorList = publicMapper.getPublicItems(paramsMap);

        operator = operatorList.get(0).get("user_name").toString();

        System.out.println("------------username:" + username);
        if (username.equals(apply_man) && (currentuser.equals(apply_man) && state.equals("打回") ||
                currentuser.equals(operator) && state.equals("审核"))) {
            //可以进行编辑或删除
            return JsonResult.success(true);
        } else {
            //不可以进行编辑或删除
            return JsonResult.success(false);
        }
    }
}
